/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class SimpleGame extends BasicGame {

    Image plane = null;
    Image plane1 = null;
    Image plane2 = null;
    Image plane3 = null;
    Image plane4 = null;
    Image plane5 = null;
    Image land = null;
    Image exp = null;
    Image puntero = null;
    //Image shot = null;
    float x = 400;
    float y = 300;
    float i = -10;
    float j = -10;
    float x2 = 400;
    float py = -200;
    float py2 = -150;
    float px1 = 100;
    float px2 = 200;
    float px3 = 300;
    float px4 = 400;
    float px5 = 500;
    float scale = 1;
    Image shot = null;
    float xf = 0;
    float yf = 0;
    Animation animation = null;
    int [] duration = {100, 100, 100, 100};
    public SimpleGame() {
        super("Slick2D Path2Glory - SlickBasicGame");
    }

    @Override
    public void init(GameContainer gc)
            throws SlickException {
        plane = new Image("src/serveis/plane.png");
        plane1 = new Image("src/serveis/plane2.png");
        plane2 = new Image("src/serveis/plane2.png");
        plane3 = new Image("src/serveis/plane2.png");
        plane4 = new Image("src/serveis/plane2.png");
        plane5 = new Image("src/serveis/plane2.png");
        land = new Image("src/serveis/fetch.jpg");
        Image exp1 = new Image("src/serveis/explosion1.png");
        Image exp2 = new Image("src/serveis/explosion2.png");
        Image exp3 = new Image("src/serveis/explosion3.png");
        Image exp4 = new Image("src/serveis/explosion4.png");
        Image[] exp = {exp1, exp1, exp1, exp2,exp2, exp2, exp3, exp3, exp3, exp4, exp4, exp4};
        shot = new Image("src/serveis/shot.jpg");
        puntero = new Image("src/serveis/shot.jpg");
        animation = new Animation(exp, duration, true);
    }

    @Override
    public void update(GameContainer gc, int delta)
            throws SlickException {
        Input input = gc.getInput();



        if (input.isKeyDown(Input.KEY_A)) {
            float hip = 0.4f * delta;
            float auxX = x;
            x -= hip;
            //y-= hip * Math.cos(Math.toRadians(rotation));
            if (x <= -30){
                x = auxX;
            }
        }

        if (input.isKeyDown(Input.KEY_D)) {
            float hip = 0.4f * delta;

            //float rotation = plane.getRotation();
            float auxX = x;

            x += hip;
            // y-= hip * Math.cos(Math.toRadians(rotation));
            if (700 <= x){
                x = auxX;
            }
        }

        if (input.isKeyDown(Input.KEY_W)) {
            float hip = 0.4f * delta;

            float rotation = plane.getRotation();
            float auxY = y;
            x += hip * Math.sin(Math.toRadians(rotation));
            y -= hip * Math.cos(Math.toRadians(rotation));
            if (y <= 0){
                y = auxY;
            }
        }
        if (input.isKeyDown(Input.KEY_S)) {
            float hip = 0.4f * delta;

            float rotation = plane.getRotation();
            float auxY = y;
            x -= hip * Math.sin(Math.toRadians(rotation));
            y += hip * Math.cos(Math.toRadians(rotation));
            if (500 <= y){
                y = auxY;
            }
        }

        if (input.isKeyDown(Input.KEY_2)) {
            scale += (scale >= 5.0f) ? 0 : 0.1f;
            plane.setCenterOfRotation(plane.getWidth() / 2.0f * scale, plane.getHeight() / 2.0f * scale);
        }
        if (input.isKeyDown(Input.KEY_1)) {
            scale -= (scale <= 1.0f) ? 0 : 0.1f;
            plane.setCenterOfRotation(plane.getWidth() / 2.0f * scale, plane.getHeight() / 2.0f * scale);
        }
        if (input.isKeyDown(Input.KEY_SPACE)) {
            i = x;
            j = y;
            scale += (scale >= 5.0f) ? 0 : 0.1f;
            shot.setCenterOfRotation(shot.getWidth() / 2.0f * scale, shot.getHeight() / 2.0f * scale);
        }
        py += (float) 2;
        py2 += (float) 2;
        j -= (float) 10;
        if (py > 600){
            py = -200;
            py2 = -150;
        }
        //yf += (float) 2;
    }

    public void render(GameContainer gc, Graphics g)
            throws SlickException {
        land.draw(xf, yf);
//
//        px3 = 200;
//        py2 = 300;

        //animation.draw(0,0);
        plane1.draw(px1, py);
        plane2.draw(px2, py + 25);
        plane3.draw(px3, py2);
        plane4.draw(px4, py + 25);
        plane5.draw(px5, py);
        //animation.draw(px3, py2);
        shot.draw(i + 58, j,scale);       //i = ancho j = alto
        if (((py2 <= j && j <= py2+100) && (px3-50 <= i && i <= px3+50))) {
            //exp.draw(px3+5, py+50);
            float auxX = px3;
            final float auxY = py2;
            py2 += 1000;
            plane3.draw(px3, py2);
            animation.draw(0,0);//auxX, auxY);
            //plane3.destroy();

        }
        plane.draw(x, y);

    }


    public static void main(String[] args)
            throws SlickException {
        AppGameContainer app = new AppGameContainer(new SimpleGame2());
        app.setDisplayMode(800, 600, false);
        app.start();
    }
}